<template>
  <div class="components-container">
    <div class="header-form" style="height:48px;">
      <div class="layui-inline" v-if="hidden == '0'">
        <label class="layui-form-label" >接尸日期</label>
        <el-date-picker v-date v-model="selectDataOne.WorkDateEquals" @change="getData" type="date" placeholder="选择接尸日期"  >
        </el-date-picker>
      </div>
      <div class="layui-inline" v-if="hidden == '1'">
        <label class="layui-form-label" >接尸日期</label>
        <el-date-picker v-date v-model="selectDataTwo.WorkDateGreaterAndEquals" type="date" placeholder="选择接尸日期" @change="getData">
        </el-date-picker>
        至
        <el-date-picker v-date v-model="selectDataTwo.WorkDateLessAndEquals" type="date" placeholder="选择接尸日期" @change="getData">
        </el-date-picker>
      </div>
      <div class="layui-inline" v-if="hidden == '2'">     
        <label class="layui-form-label" >接尸日期</label>
        <el-date-picker v-date v-model="selectDataThree.WorkDateGreaterAndEquals" type="datetime" placeholder="选择接尸日期" format="yyyy-MM-dd HH:mm" @change="getData">
        </el-date-picker>
        至
        <el-date-picker v-date v-model="selectDataThree.WorkDateLessAndEquals" type="datetime" placeholder="选择接尸日期" format="yyyy-MM-dd HH:mm" @change="getData">
        </el-date-picker>
      </div>

      <!-- <div class="layui-inline">
        <label class="layui-form-label">逝者年龄</label>
        <div class="layui-input-inline">
          <el-input type="text" v-model="PRPDEADAGE"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">逝者性别</label>
        <div class="layui-input-inline">
          <el-select v-model="PRPDEADSEX" filterable>
            <el-option v-for="item in sexType" :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue">
            </el-option>
          </el-select>
        </div>
      </div> -->
        <el-button type="primary"  @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>  
    </div>
    <div>
      <ul class="toolList">
        <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <dataTable :height="tableHeight"  :isIndex="true" :hiddenPageOrder="false" :headList="headList" :infos="infos" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <mainmodel ref="models" @refreshData="getData"></mainmodel>
     <!-- 之后的新版本  宁波个性化详情-->  
    <newmainmodel ref="newmodels" @refreshData="getData"></newmainmodel>
     <sweet-modal ref="dialogPrint" class="dialog" :sweetModalMaxWidth="pageWidth">
        <h3>{{title}}</h3>
        <iframe :src="printUrl" style="width:1100px; height:500px;margin-top:20px;"></iframe>
      </sweet-modal>
  </div>
</template>

<script>
import apiBase from "@/api/apiBase.js";
import dataTable from "@/components/Table/DataTable";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import mainmodel from "./phoneAppointmentMain";
import { PRINT } from "@/components/Pdf/PDF.js";
import { PRPORDER } from "@/const/orderConst.js";
import sweetModal from "@/components/sweetModal";
import newmainmodel from "./phoneAppointmentMainNew";
export default {
  name: "phoneAppointment",
  mixins: [manage],
  data() {
    return {
      infos: null,  // 宁波详情显示电话预约详情
      issendcar: null,
      PRPDEADAGE:null,
      PRPDEADSEX:null,
      sexType:[],
      tableHeight: "600",
      pageWidth: 1200,
      printUrl: null,
      reserveStatus: [],
      selectDataOne: {
        WorkDateEquals: new Date(),
        StatusEquals: null
      },
      selectDataTwo: {
        WorkDateGreaterAndEquals: new Date(),
        WorkDateLessAndEquals: new Date(),
        StatusEquals: null
      },
      selectDataThree: {
        WorkDateGreaterAndEquals: new Date(new Date(new Date().toLocaleDateString()).getTime()),
        WorkDateLessAndEquals: new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1),
        StatusEquals: null
      },
      hidden:'0',
    };
  },
  methods: {
    //查询条件重置
    // btnRepeat() {
    //   this.selectData = {
    //     WorkDateEquals:null,
    //      StatusEquals: null
    //    };
    //   this.getData();
    // },
    //获取电话预约数据
    getData() {
      let Data = {};
      if(this.hidden == '1') {
        Data = JSON.parse(JSON.stringify(this.selectDataTwo));
        Data.WorkDateGreaterAndEquals = formatDate(Data.WorkDateGreaterAndEquals, "yyyyMMdd");
        Data.WorkDateLessAndEquals = formatDate(Data.WorkDateLessAndEquals, "yyyyMMdd");
      } else if(this.hidden == '0') {
        Data = JSON.parse(JSON.stringify(this.selectDataOne));
        Data.WorkDateEquals = formatDate(Data.WorkDateEquals, "yyyyMMdd");
      }else if(this.hidden == '2'){
        Data = JSON.parse(JSON.stringify(this.selectDataThree));
        Data.WorkDateGreaterAndEquals = formatDate(Data.WorkDateGreaterAndEquals, "yyyyMMdd");
        Data.WorkTimeGreaterAndEquals = this.selectDataThree.WorkDateGreaterAndEquals==null?null:formatDate(this.selectDataThree.WorkDateGreaterAndEquals, "hhmm") +'00';
        Data.WorkDateLessAndEquals = formatDate(Data.WorkDateLessAndEquals, "yyyyMMdd");
        Data.WorkTimeLessAndEquals = formatDate(this.selectDataThree.WorkDateLessAndEquals, "hhmm")==null?null:formatDate(this.selectDataThree.WorkDateLessAndEquals, "hhmm")+"00";
      }
        let data = {
          PageIndex: 1,
          PageSize: 200,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        if (Data.WorkDateEquals == null && Data.WorkDateGreaterAndEquals == null && Data.WorkDateLessAndEquals == null)
          this.$message("接尸日期为空，限查200条！");
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Reserve.QueryPhoneRecord,
          res => {
            this.dataList = res.data.content.OutData;
            this.pageOrder.totalNum = res.data.content.RecordCount;
            this.dataList.forEach(data => {
              data.PRPADDDATE = data.PRPADDDATE.substring(0, 16);
              data.PRPWORKDATE = data.PRPWORKDATE.substring(0, 16);
              data.PPRWORKTIME = data.PPRWORKTIME.substring(0, 5);
            });
          }
        );
    },
    //获取性别
    getSexType() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Code.GetSex, res => {
        this.sexType = res.data.content;
      });
    },
    //宁波详情个性化
    GetIdvDetail() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Param.GetIdvDetail,
        res => {
          this.issendcar = res.data.content; //是否显示保存按钮
          if (this.issendcar === "10") {
            this.infos = "详情";
          } else if (this.issendcar === "20") {   //宁波需求的详情页
            this.infos = "义乌详情";
          }
        }
      );
    },
    //获取表头
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetTelephoneRecordColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    btnShow(btn){
      switch (btn) {
        case '修改':
          if(this.currentRow)
          return true;
          break;
        case '打印':
          return true;
          break;
        case '导出':
          return true;
          break;
        default:
      }
    },
    //功能操作
    operate(order) {
      this.order = order;
      console.log(this.order);//打印
      switch (this.order) {
       case "义乌详情":
          this.$refs.newmodels.open(this.order, this.currentRow.PRPID);
          break;
        case PRPORDER.INFO://详情
          this.$refs.models.open(this.order, this.currentRow.PRPID);
          break;
        case PRINT.PRINT://打印
          this.title = "打印电话记录";
          this.getPhoneRecorder();
          break;
        case PRINT.PHONERECORDER://导出 
          this.phoneRecorder();//导出excel表
          break;
        case '修改'://修改 
          this.$refs.models.open(this.order, this.currentRow.PRPID); 
          break;
        default:
      }
    },
    //提交
    submit() {
      switch (this.order) {
        case INFO:
          this.$refs.dialog.close();
          break;
        default:
      }
    },
    //打印
    getPhoneRecorder() {
      if (this.selectDataOne.WorkDateEquals != null || (this.selectDataTwo.WorkDateGreaterAndEquals != null && this.selectDataTwo.WorkDateLessAndEquals != null)) {
        let Data = {};
      if(this.hidden == '1') {
        Data = JSON.parse(JSON.stringify(this.selectDataTwo));
        Data.WorkDateGreaterAndEquals = formatDate(Data.WorkDateGreaterAndEquals, "yyyyMMdd");
        Data.WorkDateLessAndEquals = formatDate(Data.WorkDateLessAndEquals, "yyyyMMdd");
      } else if(this.hidden == '0') {
        Data = JSON.parse(JSON.stringify(this.selectDataOne));
        Data.WorkDateEquals = formatDate(Data.WorkDateEquals, "yyyyMMdd");
      }else if(this.hidden == '2'){
         Data = JSON.parse(JSON.stringify(this.selectDataThree));
        Data.WorkDateGreaterAndEquals = formatDate(Data.WorkDateGreaterAndEquals, "yyyyMMdd");
        Data.WorkTimeGreaterAndEquals = this.selectDataThree.WorkDateGreaterAndEquals==null?null:formatDate(this.selectDataThree.WorkDateGreaterAndEquals, "hhmm") +'00';
        Data.WorkDateLessAndEquals = formatDate(Data.WorkDateLessAndEquals, "yyyyMMdd");
        Data.WorkTimeLessAndEquals = formatDate(this.selectDataThree.WorkDateLessAndEquals, "hhmm")==null?null:formatDate(this.selectDataThree.WorkDateLessAndEquals, "hhmm")+"00";
      }
        let data = {
          PageIndex: -1,
          PageSize: 0,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Pdf.PrintTelephonerecord,
          res => {
            this.printUrl = res.data.content.PDFNAME;
            this.$refs.dialogPrint.open();
          }
        );
      } else {
        alert("接尸日期为空");
      }
    },
    //导出excel表
    phoneRecorder(){
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      let time = null;
      if(this.hidden == '1'){
        let time1 = this.selectDataTwo.WorkDateGreaterAndEquals?formatDate(this.selectDataTwo.WorkDateGreaterAndEquals, "yyyy-MM-dd"):'空'
        let time2 = this.selectDataTwo.WorkDateLessAndEquals?formatDate(this.selectDataTwo.WorkDateLessAndEquals, "yyyy-MM-dd"):'空'
        time = time1 + '_' + time2
      }else if(this.hidden == '0'){
          time = this.selectDataOne.WorkDateEquals?formatDate(this.selectDataOne.WorkDateEquals, "yyyy-MM-dd"):'空'
      }else if(this.hidden == '2'){
        let time1 = this.selectDataTwo.WorkDateGreaterAndEquals?formatDate(this.selectDataTwo.WorkDateGreaterAndEquals, "yyyy-MM-dd hh:mm"):'空'
        let time2 = this.selectDataTwo.WorkDateLessAndEquals?formatDate(this.selectDataTwo.WorkDateLessAndEquals, "yyyy-MM-dd hh:mm"):'空'
        time = time1 + '_' + time2
      }
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require('@/vendor/Export2Excel');
        export_json_to_excel(this.headList, this.dataList, `电话记录统计表 ${time}`);
      this.downloadLoading = false;
      });
    },
    //获取预约状态
    getReserveStatus() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Reserve.GetReserveStatus,
        res => {
          this.reserveStatus = res.data.content;
        }
      );
    },
    // btnSearch() {
    //   this.getData();
    // }
  },
  // mounted() {
  //   this.getPhoneRecorder();
  // },
  components: {
    dataTable,
    mainmodel,
    sweetModal,
    newmainmodel,
  },
  mounted() {
    this.selectData.StatusEquals = this.all;
  },
  created() {
    apiBase.DoService(
        {},
        apiBase.getApiAdress().Param.GetIsShowWorkDate,
        res => {
          if(res.data){
            this.hidden = res.data.content;
            this.getData();
          }
        }
    );
    this.getColumns();
    this.getReserveStatus();
    this.getSexType();
    this.GetIdvDetail();
  },
  //路由的时候刷新组件
  activated: function() {
    this.getData();
  }

};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>